import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import RewardsModel 1.0

GroupBox {
    title: "奖励管理"

    property RewardsModel rewardsModel

    ColumnLayout {
        width: parent.width
        spacing: 10

        // 奖励添加部分
        GroupBox {
            title: "添加奖励"
            Layout.fillWidth: true

            GridLayout {
                columns: 2
                width: parent.width

                Label { text: "奖励名称:" }
                TextField {
                    id: rewardNameField
                    Layout.fillWidth: true
                    placeholderText: "输入奖励名称"
                }

                Label { text: "所需积分:" }
                SpinBox {
                    id: rewardsPoints
                    Layout.fillWidth: true
                    from: 0
                    to: 100000
                    value: 100
                }

                Item { Layout.fillWidth: true; Layout.columnSpan: 1 }
                Button {
                    text: "添加奖励"
                    Layout.fillWidth: true
                    Layout.columnSpan: 1
                    onClicked: {
                        if (rewardNameField.text.trim() === "") {
                            rewardsMessage.text = "错误：奖励名称不能为空！"
                            rewardsMessage.color = "red"
                            rewardsMessage.visible = true
                            rewardsMessageTimer.restart()
                            return
                        }

                        rewardsModel.addReward(rewardNameField.text, rewardsPoints.value);

                        rewardsMessage.text = "奖励添加成功！"
                        rewardsMessage.color = "green"
                        rewardsMessage.visible = true
                        rewardsMessageTimer.restart()

                        // 清空输入
                        rewardNameField.text = ""
                        rewardsPoints.value = 100
                    }
                }
            }
        }

        // 奖励列表
        GroupBox {
            title: "奖励列表"
            Layout.fillWidth: true

            ColumnLayout {
                width: parent.width
                spacing: 5

                RowLayout {
                    Button {
                        text: "保存奖励数据"
                        onClicked: {
                            rewardsModel.saveRewardData()
                            rewardsMessage.text = "奖励数据保存成功！"
                            rewardsMessage.color = "green"
                            rewardsMessage.visible = true
                            rewardsMessageTimer.restart()
                        }
                    }

                    Button {
                        text: "加载奖励数据"
                        onClicked: {
                            rewardsModel.loadRewardData()
                            rewardsMessage.text = "奖励数据加载成功！"
                            rewardsMessage.color = "blue"
                            rewardsMessage.visible = true
                            rewardsMessageTimer.restart()
                        }
                    }
                }

                ListView {
                    id: rewardsListView
                    Layout.fillWidth: true
                    Layout.preferredHeight: 200
                    model: rewardsModel
                    delegate: RewardItemDelegate {
                        kidRewardsModel: rewardsModel
                        onRewardExchanged:(message, color) =>{
                            rewardsMessage.text = message
                            rewardsMessage.color = color
                            rewardsMessage.visible = true
                            rewardsMessageTimer.restart()
                        }
                    }
                }
            }
        }

        // 奖励操作消息
        Label {
            id: rewardsMessage
            Layout.fillWidth: true
            visible: false
            horizontalAlignment: Text.AlignHCenter
            wrapMode: Text.Wrap
        }
    }

    Timer {
        id: rewardsMessageTimer
        interval: 3000
        onTriggered: rewardsMessage.visible = false
    }
}
